package servlets;

import java.io.IOException;
import java.io.StringReader;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import net.webservicex.GlobalWeather;
import net.webservicex.GlobalWeatherSoap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * Servlet implementation class WeatherServlet
 */
public class WeatherServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		RequestDispatcher dispatcher = request
				.getRequestDispatcher("pages/weather.jsp");
		dispatcher.forward(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String city = request.getParameter("city");
		if (city == null || city.equals(""))
			return;

		GlobalWeather client = new GlobalWeather();
		GlobalWeatherSoap service = client.getGlobalWeatherSoap();
		String str = service.getWeather(city, "");

		StringBuilder info = new StringBuilder();
		if (str.equals("Data not found"))
			info.append("No");
		else {
			DocumentBuilder parser;
			info.append("<hr/><table id=\"container\">");
			try {
				parser = DocumentBuilderFactory.newInstance()
						.newDocumentBuilder();
				Document document = parser.parse(new InputSource(
						new StringReader(str)));
				document.getDocumentElement().normalize();
				NodeList nList = document
						.getElementsByTagName("CurrentWeather");
				for (int temp = 0; temp < nList.getLength(); temp++) {
					Node nNode = nList.item(temp);
					if (nNode.getNodeType() == Node.ELEMENT_NODE) {
						Element eElement = (Element) nNode;

						info.append("<tr><td>City: </td><td>"
								+ getTagValue("Location", eElement));
						info.append("</td></tr>");
						info.append("<tr><td>Time: </td> <td>"
								+ getTagValue("Time", eElement));
						info.append("</td></tr>");
						info.append("<tr><td>Wind: </td><td> "
								+ getTagValue("Wind", eElement));
						info.append("</td></tr>");
						info.append("<tr><td>Visibility: </td><td> "
								+ getTagValue("Visibility", eElement));
						info.append("</td></tr>");
						info.append("<tr><td>Temperature: </td> <td>"
								+ getTagValue("Temperature", eElement));
						info.append("</td></tr>");
						info.append("<tr><td>DewPoint: </td><td> "
								+ getTagValue("DewPoint", eElement));
						info.append("</td></tr>");
						info.append("<tr><td>RelativeHumidity: </td><td> "
								+ getTagValue("RelativeHumidity", eElement));
						info.append("</td></tr>");
						info.append("<tr><td>Pressure: </td><td> "
								+ getTagValue("Pressure", eElement));
						info.append("</td></tr>");

					}
				}
				info.append("</table><hr/>");
			} catch (ParserConfigurationException e) {
				e.printStackTrace();
			} catch (SAXException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		request.setAttribute("info", info.toString());
		RequestDispatcher dispatcher = request
				.getRequestDispatcher("pages/weather.jsp");
		dispatcher.forward(request, response);
	}

	private String getTagValue(String sTag, Element eElement) {
		NodeList nlList = eElement.getElementsByTagName(sTag).item(0)
				.getChildNodes();

		Node nValue = (Node) nlList.item(0);

		return nValue.getNodeValue();
	}
}
